﻿#region Opis pliku
//-----------------------------------------------------------------------------
// MenuItem.cs
// Autor: Jan Czarnowski
// 31.10.2012
//-----------------------------------------------------------------------------
#endregion

#region Using...

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa reprezentująca element menu.
    /// </summary>
    public class MenuItem
    {
        #region pola

        string text;
        Menu menu;
        Color color = Color.Black;
        Rectangle bounds;
        bool isSelected = false;
        bool isPressed = false;
        TimeSpan timePressed;

        #endregion

        #region wlasciwosci

        /// <summary>
        /// Obszar, który ten element zajmuje
        /// </summary>
        public Rectangle Bounds
        {
            get { return bounds; }
            set { bounds = value; }
        }

        public bool IsPressed
        {
            get {
                if (isPressed)
                {
                    isPressed = false;
                    return true;
                }
                else
                    return false;
            }
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        #endregion

        #region inicjalizacja

        /// <summary>
        /// Tworzy nowy obiekt MenuItem
        /// </summary>
        /// <param name="text">tytuł elementu</param>
        /// <param name="bounds">Prostokąt, który element zajmuje</param>
        public MenuItem(string text, Rectangle bounds, Menu menu)
        {
            this.text = text;
            this.bounds = bounds;
            this.menu = menu;
            this.color = Color.Black;
        }

        #endregion

        #region metody draw i update

        public void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            SpriteFont font = menu.ScreenManager.Font;
            Vector2 textSize = font.MeasureString(text);

            int squareSide = 8;
            Rectangle square = new Rectangle(bounds.X + 15, bounds.Y + (bounds.Height - squareSide)/2, squareSide, squareSide);
            exSpriteBatch.FillRectangle(square, color);

            //exSpriteBatch.DrawString(font, "#", new Vector2(bounds.X + 15, bounds.Y + (bounds.Height - font.MeasureString("#").Y) / 2), color);
            
            int gapSize = 15;
            //exSpriteBatch.DrawString(font, text, new Vector2(bounds.X + 15 + font.MeasureString("#").X + gapSize, bounds.Y + (bounds.Height - textSize.Y) / 2 + 1), color);

            exSpriteBatch.DrawString(font, text, new Vector2(square.Right + gapSize, bounds.Y + (bounds.Height - textSize.Y) / 2 + 1), color);
            //exSpriteBatch.DrawRectangle(bounds, color);
        }

        public void Update(GameTime gameTime)
        {
            if ((timePressed -= gameTime.ElapsedGameTime) <= TimeSpan.Zero)
            {
                isPressed = false;
                color = Color.Black;
            }
        }

        #endregion

        #region metody publiczne

        public void HandleInput(InputState input)
        {
            Point mousePos = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y);

            if (bounds.Contains(mousePos))
            {
                isSelected = true;

                if(!isPressed)
                    color = Color.Yellow;

                if (input.IsNewMLeftButtonPressed())
                {
                    isPressed = true;
                    timePressed = TimeSpan.FromMilliseconds(500);

                    color = Color.Red;
                }
            }
            else
            {
                isSelected = false;
                if(!isPressed)
                    color = Color.Black;
            }
        }

        #endregion
    }
}
